<?php

namespace app\api\module;

use app\common\model\Coupon;
use app\common\model\Express;
use app\common\model\GoodsComment;
use app\common\model\Order as OrderModel;
use app\common\model\OrderChange;
use app\common\model\OrderNew;
use app\common\model\OrderRefund;
use app\common\model\OrderVerify;
use app\common\model\Settings;
use think\Db;

class Order extends Common
{
    /**
     * 判断是否需要登录
     *
     * @param string $interface 待检测的接口
     * @return bool
     */
    protected function needLogin($interface)
    {
        //不需登录的接口
        $ignoreList = [
            ZFN_ORDER_PAY_AD,
        ];

        if (in_array($interface, $ignoreList)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 执行接口操作
     *
     * @param $interface
     * @return array|string
     */
    public function exec($interface)
    {
        switch ($interface) {

            //【1001】好物确认订单
            case ZFN_ORDER_THING_CONFIRM:
                return $this->thingConfirm();
            //【1002】团购确认订单
            case ZFN_ORDER_GROUP_CONFIRM:
                return $this->groupConfirm();
            //【1003】好物提交订单
            case ZFN_ORDER_THING_SUB:
                return $this->thingSub();
            //【1004】团购提交订单
            case ZFN_ORDER_GROUP_SUB:
                return $this->groupSub();
            //【1005】订单支付
            case ZFN_ORDER_PAY_INFO:
                return $this->payInfo();
            //【1006】订单支付成功广告
            case ZFN_ORDER_PAY_AD:
                return $this->payAd();
            //【1007】订单列表
            case ZFN_ORDER_LIST:
                return $this->orderList();
            //【1008】取消订单
            case ZFN_ORDER_CANCEL:
                return $this->orderCancel();
            //【1009】订单获取支付弹窗信息
            case ZFN_ORDER_PAY_ALERT:
                return $this->payAlert();
            //【1010】订单前端删除
            case ZFN_ORDER_APP_REMOVE:
                return $this->appRemove();
            //【1011】订单物流信息
            case ZFN_ORDER_EXPRESS:
                return $this->expressInfo();
            //【1012】订单前端删除
            case ZFN_ORDER_CONFIRM_RECEIVE:
                return $this->confirmReceive();
            //【1013】查看券码
            case ZFN_ORDER_VERIFY_CODE:
                return $this->verifyCode();
            //【1014】订单评价拉取信息
            case ZFN_ORDER_COMMENT_INFO:
                return $this->commentInfo();
            //【1015】订单评价提交
            case ZFN_ORDER_COMMENT_SUB:
                return $this->commentSub();
            //【1016】订单详情
            case ZFN_ORDER_DETAIL:
                return $this->orderDetail();
            //【1017】订单退款原因
            case ZFN_ORDER_REFUND_REASON:
                return $this->refundReason();
            //【1018】申请退款
            case ZFN_ORDER_REFUND_APPLY:
                return $this->refundApply();
            //【1019】申请换货
            case ZFN_ORDER_CHANGE_APPLY:
                return $this->changeApply();
            //【1020】核销页面
            case ZFN_ORDER_VERIFY_PAGE:
                return $this->verifyPage();
            //【1021】核销提交
            case ZFN_ORDER_VERIFY_SUB:
                return $this->verifySub();
            //【1022】获取当前商品可用的优惠券
            case WJH_ORDER_COUPON:
                return $this->order_coupon();
            //【1023】账单
            case WJH_ORDER_BILL:
                return $this->order_bill();
            //【1024】账单搜索发放邮件
            case WJH_ORDER_BILL_SEND:
                return $this->order_bill_send();
            //【1025】账单支付
            case WJH_ORDER_BILL_PAY:
                return $this->order_bill_pay();
            //【1026】账单支付
            case WJH_FOLLOW_ORDER:
                return $this->order_follow_order();
            //【1027】获取支付类型
            case WJH_ORDER_PAY_TYPE:
                return $this->order_pay_type();
            default:
                send_developer_error("模块【订单】中不存在接口【" . $interface . "】,核实文档后重试!");
        }
    }

    private function thingConfirm(){
        $user = $this->currentUser();
        $car = $this->essentialParam('car');
        $address_id = $this->optionalParam('address_id');
        $source = $this->essentialParam('source');
        $data = OrderNew::thingConfirm($user,$car,$address_id,$source);
        $this->insertResponse('',$data);
        return $this->response('好物确认订单');
    }

    private function groupConfirm(){
        $user = $this->currentUser();
        $car = $this->essentialParam('car');
        $longitude = $this->optionalParam('longitude');
        $latitude = $this->optionalParam('latitude');
        $source = $this->essentialParam('source');
        $data = OrderNew::groupConfirm($user,$car,$longitude,$latitude,$source);
        $this->insertResponse('',$data);
        return $this->response('团购确认订单');
    }

    private function thingSub(){
        $user = $this->currentUser();
        $car = $this->essentialParam('car');
        $from = $this->essentialParam('from');
        $address_id = $this->essentialParam('address_id');
        $use_coin = $this->optionalParam('use_coin');
        $note = $this->optionalParam('note');
        $coupon = $this->optionalParam('coupon');
        $pay_type = $this->optionalParam('pay_type');
        $data = OrderNew::thingSub($user,$car,$from,$address_id,$use_coin,$note,$pay_type,$coupon);
        $this->insertResponse('',$data);
        return $this->response('好物提交订单');
    }

    private function groupSub(){
        $user = $this->currentUser();
        $car = $this->essentialParam('car');
        $from = $this->essentialParam('from');
        $use_coin = $this->optionalParam('use_coin');
        $note = $this->optionalParam('note');
        $pay_type = $this->optionalParam('pay_type');
        $data = OrderNew::groupSub($user,$car,$from,$use_coin,$note,$pay_type);
        $this->insertResponse('',$data);
        return $this->response('团购提交订单');
    }

    private function payInfo(){
        $user = $this->currentUser(true);
        $order_sn = $this->essentialParam('order_sn');
        $pay_type = $this->optionalParam('pay_type');
        $source = $this->essentialParam('source');
        $data = OrderModel::payInfo($user,$order_sn,$pay_type,$source);
        $this->insertResponse('',$data);
        return $this->response('订单支付信息');
    }

    private function payAd(){
        $type = $this->essentialParam('type');
        $data = OrderNew::payAd($type);
        $this->insertResponse('',$data);
        return $this->response('支付广告');
    }

    private function orderList(){
        $user = $this->currentUser();
        $tag = $this->optionalParam('tag');
        $page = $this->essentialParam('page');
        $order_sn = $this->optionalParam('order_sn');
        $page_size = $this->essentialParam('page_size');
        $data = OrderModel::lists($user,$tag,$page,$page_size,$order_sn);
        $this->insertResponse('data',$data);
        return $this->response('订单列表');
    }

    //订单取消
    private function orderCancel(){
        $user = $this->currentUser(true);
        $order_id = $this->essentialParam('order_id');
        OrderModel::cancel($user['id'],$order_id);
        return $this->response('订单取消成功');
    }

    //订单付款信息
    private function payAlert(){
        $user = $this->currentUser(true);
        $order_id = $this->essentialParam('order_id');
        $source = $this->essentialParam('source');
        $data = OrderModel::payPage($user,$order_id,$source);
        $this->insertResponse('',$data);
        return $this->response('订单支付弹窗信息');
    }

    private function appRemove(){
        $user = $this->currentUser(true);
        $order_id = $this->essentialParam('order_id');
        OrderModel::orderAppRemove($user,$order_id);
        return $this->response('订单删除成功');
    }

    private function expressInfo(){
        $user = $this->currentUser();
        $order_id = $this->essentialParam('order_id');
        $data = OrderModel::expressInfo($order_id,$user);
        $this->insertResponse('',$data);
        return $this->response('物流信息');
    }

    //订单确认收货
    private function confirmReceive(){
        $user = $this->currentUser();
        $order_id = $this->essentialParam('order_id');
        OrderModel::confirmReceive($order_id,$user);
        return $this->response('订单确认收货成功');
    }

    private function verifyCode(){
        $user = $this->currentUser();
        $order_id = $this->essentialParam('order_id');
        $data = OrderModel::verifyCode($user,$order_id);
        $this->insertResponse('',$data);
        return $this->response('核销券码');
    }

    private function commentInfo(){
        $user = $this->currentUser();
        $order_id = $this->essentialParam('order_id');
        $data = GoodsComment::commentInfo($user,$order_id);
        $this->insertResponse('',$data);
        return $this->response('评价拉取信息');
    }

    private function commentSub(){
        $user = $this->currentUser();
        $order_id = $this->essentialParam('order_id');
        $data = $this->essentialParam('data');
        GoodsComment::commentSub($user,$order_id,$data);
        return $this->response('评价成功');
    }

    private function orderDetail(){
        $user = $this->currentUser();
        $order_id = $this->essentialParam('order_id');
        $longitude = $this->optionalParam('longitude');
        $latitude = $this->optionalParam('latitude');
        $data = OrderModel::detail($user,$order_id,$longitude,$latitude);
        $this->insertResponse('',$data);
        return $this->response('订单详情');
    }

    private function refundReason(){
        $user = $this->currentUser();
        $order_id = $this->essentialParam('order_id');
        $data = OrderRefund::reason($user,$order_id);
        $this->insertResponse('',$data);
        return $this->response('订单退款信息');
    }

    private function refundApply(){
        $user = $this->currentUser();
        $order_id = $this->essentialParam('order_id');
        $reason_id = $this->optionalParam('reason_id');
        $note = $this->optionalParam('note');
        $imgs = $this->optionalParam('imgs');
        OrderRefund::refundApply($user,$order_id,$reason_id,$note,$imgs);
        return $this->response('退款申请已提交，请等待商家处理');
    }

    private function changeApply(){
        $user = $this->currentUser();
        $order_id = $this->essentialParam('order_id');
        $note = $this->optionalParam('note');
        $imgs = $this->optionalParam('imgs');
        OrderChange::changeApply($user,$order_id,$note,$imgs);
        return $this->response('换货申请已提交，请等待商家处理');
    }

    private function verifyPage(){
        $user = $this->currentUser();
        $order_id = $this->essentialParam('order_id');
        $data = OrderVerify::verifyPage($order_id,$user);
        $this->insertResponse('',$data);
        return $this->response('核销页面信息');
    }

    private function verifySub(){
        $user = $this->currentUser();
        $order_id = $this->essentialParam('order_id');
        $verify_code = $this->essentialParam('verify_code');
        $num = $this->optionalParam('num');
        OrderVerify::verifySub($user,$order_id,$verify_code,$num,1);
        return $this->response('核销成功');
    }
    private function order_coupon(){
        $user = $this->currentUser();
        $goods_list = $this->essentialParam('goods_list');
        $goods_price = $this->essentialParam('goods_price');
        $data=Coupon::getgoodcoupon($user['id'],$goods_list,$goods_price);
        $this->insertResponse('',$data);
        return $this->response('获取成功');
    }
    private function order_bill(){
        $user = $this->currentUser();
        $tag = $this->optionalParam('tag');
        $page = $this->essentialParam('page');
        $order_sn = $this->optionalParam('order_sn');
        $page_size = $this->essentialParam('page_size');
        $date = $this->essentialParam('date');
        $data = OrderModel::bills_lists($user,$tag,$page,$page_size,$order_sn,$date);
        $this->insertResponse('',$data);
        return $this->response('获取成功');
    }
    private function order_bill_send(){
        $user = $this->currentUser();
        $tomail = $this->essentialParam('tomail');
        $firsttime = $this->essentialParam('firsttime');
        $endtime = $this->essentialParam('endtime');
        $data = OrderModel::bills_lists_send($user,$tomail,$firsttime,$endtime);
        $this->insertResponse('',$data);
        return $this->response('发送成功');
    }
    private function order_bill_pay(){
        $user = $this->currentUser();
        $pay_type = $this->essentialParam('pay_type');
        $data = OrderModel::bill_order($user,$pay_type);
        $this->insertResponse('',$data);
        return $this->response('支付成功');
    }
    private function order_follow_order(){
        $user = $this->currentUser();
        $order_id = $this->essentialParam('order_id');
        $data = OrderModel::expressInfo($order_id,$user);
        $this->insertResponse('',$data);
        return $this->response('获取成功');
    }
    private function order_pay_type(){
        $type= $this->essentialParam('type');
        $data = OrderModel::get_order_pay_type($type);
        $this->insertResponse('',$data);
        return $this->response('获取成功');
    }
}